<?php
/**
 * Object Relational Persistence Mapping Class for table: ticket_insurance_info
 *
 * @package	com.athena
 * @author	LHV
 */
class TicketInsurance extends ORDataObject {

	/**#@+
	 * Fields of table: ticket_insurance_info mapped to class members
	 */
	var $ticket_insurance_id     = '';
	var $ticket_id		         = '';
	var $insurance_id		     = '';
	var $correct_route		     = '';
	var $paid_for_36_month		 = '';
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'ticket_insurance';

	/**
	 * Primary Key
	 */
	var $_key = 'ticket_insurance_id';

	/**
	 * Internal Name
	 */
	var $_internalName = 'TicketInsurance';

	/**
	 * Handle instantiation
	 */
	function __construct() {
		parent::ORDataObject();
	}

	public function getLatestObjectByTicket($ticketId) {
		$sql  =   "SELECT MAX($this->_key) id FROM $this->_table WHERE ticket_id='$ticketId'";
		$res  =   $this->_execute($sql);
		$obj  =   Celini::newORDO('TicketInsurance', $res->fields["id"]);
		return $obj;
	}

	public function getCorrectRoute() {
		return $this->correct_route;
	}

	public function getPaidFor36Month() {
		return $this->paid_for_36_month;
	}

	public function getInsuranceInfoByTicket($ticketId, $dateToLocalized = false) {
        $field      =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
        $format     =   L10nHelper::getSqlDateFormat();
        $sql    =   array(
            'cols'  => "t.ticket_id, t.insurance_id, i.insurance_code, i.date_issued, i.date_expired, i.registered_hospital_id,
                        t.correct_route, t.paid_for_36_month, h.$field hospital_name",
            'from'  =>  "$this->_table t
                        INNER JOIN insurance i ON (t.insurance_id = i.insurance_id AND t.ticket_id = '$ticketId')
                        INNER JOIN hospital h ON (i.registered_hospital_id = h.hospital_id)"
        );
        if ($dateToLocalized) {
            $sql['cols']    =   "t.ticket_id, t.insurance_id, insurance_code,
                                DATE_FORMAT(date_issued, '$format') date_issued, DATE_FORMAT(date_expired, '$format') date_expired, registered_hospital_id,
                                correct_route, paid_for_36_month, h.$field hospital_name";
        }
        $ds =   &new Datasource_sql();
        $ds->setup($this->_db, $sql);
        return $ds->toArray();
	}

	public function deleteByTicket($ticketId) {
		$sql  =   "DELETE FROM $this->_table WHERE ticket_id='$ticketId'";
		return $this->_execute($sql);
	}

	public function setData($ticketId, $insuranceId, $correctRoute, $paidFor36Months) {
		$data =   array(
            'ticket_id'     =>  $ticketId,
		    'insurance_id'  =>  $insuranceId,
            'correct_route' =>  $correctRoute,
            'paid_for_36_month' =>  $paidFor36Months
		);
		$this->populateArray($data);
	}
}
?>
